/**
 * 基础封装工具类
 */
//指定名空间（封装性）
$package("SkyCity");
function $package(ns) {
    var a = arguments, o = null, i, j, d;
    for(i=0; i<a.length;i=i+1){
        d = (""+a[i]).split(".");
        o = window;
        for(j=0;j<d.length;j = j+1){
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return 0;
}

//获取项目根路径
function getRootPath(){
    var pathName = window.location.pathname.substring(1);
    var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
    var appName = '';
    if(webName && webName!=''){
        appName = webName+'/';
    } else {
        appName = "";
    }
    //window.location.protocol->'http:'
    //window.location.host->'127.0.0.1:8088'
    return window.location.protocol + '//' + window.location.host + '/'+ appName;

}
// 全局根路径
var wwwroot=getRootPath();

/**
 * 数组转成树型结构
 *  {
 * 		parentField : "pid",
 * 		textField : "name",
 * 		idField : "key",
 * 		childrenField:"children"
 * 	}
 * parentField : "pid",
 * textField : "name",
 * idField : "key"
 * textName: 可不设置，设置后会为每个对象增加一个对应设置的属性，并存textField设置的值
 */
Array.prototype.toTree = function(opt) {
    if(!opt){
        opt = {parentField : "pid",textField : "name",idField : "key",childrenField:"children"}
    }
    var data = [];
    var i, l, treeData = [], tmpMap = [];

    for (i = 0, l = this.length; i < l; i++) {
        data[i] = this[i];
        if (opt.textName) {
            data[i][opt.textName] = data[i][opt.textField];
        }
        //data[i]["type"] = "file";
        tmpMap[data[i][opt.idField]] = data[i];
    }

    for (i = 0, l = data.length; i < l; i++) {
        if (tmpMap[data[i][opt.parentField]] && data[i][opt.idField] != data[i][opt.parentField]) {
            if (!tmpMap[data[i][opt.parentField]][opt.childrenField]){
                tmpMap[data[i][opt.parentField]][opt.childrenField] = [];
                tmpMap[data[i][opt.parentField]]['group']=true;
            }
            tmpMap[data[i][opt.parentField]][opt.childrenField].push(data[i]);
        } else {
            treeData.push(data[i]);
        }
    }
    return treeData;
};

/**
 * 数组转成字典
 * keyField : 设置字典keyField对应的字段名，或获取keyField的方法
 */
Array.prototype.toDict = function(keyField, copy) {
    var data=new Array();
    var copyData = this;
    if(copy == undefined || copy == true){
        copyData = $.extend(true, [],  this);
    }

    if (typeof keyField === 'string'){
        var  len = copyData.length;
        for (var i = 0; i < len; i++) {
            data[copyData[i][keyField]] = copyData[i];
        }
    }else if(typeof keyField === 'function'){
        var len = copyData.length;
        for (var i = 0; i < len; i++) {
            data[keyField.call(copyData, copyData[i], i)] = copyData[i];
        }
    }else{
        return undefind;
    }
    return data;
};

/**
 * 从字典中获取值
 * key : 设置字典key对应的字段名，或获取key的方法
 */
Array.prototype.getText = function(key,valueField) {
    var value = '';
    var valueObj = this[key];
    if(valueObj){
        value = valueObj[valueField];
    }else{
        value = 'error';
    }
    return value;
};

/**
 * 从数据表格字段中获取选中字段
 */

Array.prototype.removeBykey = function(keyField, value) {
    var data=[];
    var objData = this;
    var len = objData.length;
    for(var i = 0 ; i < len ; i++){
        if(objData[i][keyField] != value){
            data.push(objData[i]);
        }
    }
    return data;
};

Array.prototype.removeByArray = function(srcKey,removeKey, removeArr) {
    var data=[];
    var objData = this;
    var len = objData.length;
    var lenR = removeArr.length;
    for(var i = 0 ; i < len ; i++){
        var flag = true;
        for(var j = 0 ; j < lenR ; j++){
            if(objData[i][srcKey] == removeArr[j][removeKey]){
                flag = false;
            }
        }

        if(flag){
            data.push(objData[i]);
        }
    }
    return data;
};
Array.prototype.addAll = function(key,newArr) {
    var data=[];
    var objData = this;
    var len = objData.length;
    for(var i = 0 ; i < len ; i++){
        data.push(objData[i]);
    }

    var newlen = newArr.length;
    for(var i = 0 ; i < newlen ; i++){
        var value = newArr[i][key];
        var dataLen = data.length;
        var falg = true;
        for(var j = 0 ; j < dataLen ; j++){
            if(data[j][key] == value){
                falg = false;
                break;
            }
        }
        if(falg){
            data.push(newArr[i]);
        }
    }
    return data;
};

Array.prototype.getBykey = function(value,key) {
    if(!key || key==''){
        key='id';
    }
    var obj = null;
    var data = this;
    var len = data.length;
    for(var i = 0 ; i < len ; i++){
        if(data[i][key] == value){
            obj = data[i];
            break;
        }
    }
    return obj;
};

Array.prototype.getByValue = function(value,key,childrenkey){
    if(!key || key==''){
        key='id';
    }
    if(!childrenkey || childrenkey==''){
        childrenkey='children';
    }
    var obj = null;
    var data = this;

    var len = data.length;
    for(var i = 0 ; i < len ; i++){
        if(data[i][key] == value){
            obj = data[i];
        }else{
            var a = data[i][childrenkey];
            if(a){
                obj = a.getByValue(value,key,childrenkey);
            }
        }
        if(obj){
            break;
        }
    }
    return obj;
};

/**
 * 数组分组
 * @param groupcol 分组字段
 * @returns {*}
 */
Array.prototype.toGroup = function(groupcol){
    var newData = new Array();
    var data = this;
    var len = data.length;
    for(var i = 0 ; i < len ; i++){
        var tempKey = data[i][groupcol];
        var groupData = newData[tempKey];
        if(!groupData){
            groupData = new Array();
        }
        groupData.push(data[i]);
        newData[tempKey] = groupData;
    }
    return newData;
};



/**
 * 数组分组二
 * @param groupcol 分组字串
 * @returns {*}
 */
Array.prototype.toGroup2 = function(groupcol){
    var newData = new Array();
    var data = this;
    var len = data.length;
    for(var i = 0 ; i < len ; i++){

        var flag = false;
        for(var name in data[i]){
            if('rowkey' != name && name.indexOf(groupcol)!=-1){
                flag = true;
                break;
            }
        }
        if(flag){
            newData.push(data[i]);
        }
    }
    return newData;
};

/**
 * 数组分组三
 * @param groupcol 分组字串
 * @returns {*}
 */
Array.prototype.toGroup3 = function (groupcol) {
    var newArr=new Array();
    var data = this;
    var newObj={};
    for (var name in data[0]) {
        if ('rowkey' != name && name.indexOf(groupcol) != -1) {
            newObj[name]=data[0][name];
        }
    }
    newArr.push(newObj);
    return newArr;
};
Array.prototype.initCheckedFlag= function(){
    var data = this;
    if(data){
        var len = data.length;
        for(var i = 0 ; i < len ; i++){
            data[i].checkFlag = false;
        }
    }
    return data;
};

/**
 * 空判断
 * @param n
 * @returns {boolean}
 */
function isNull(n){
    if(n != undefined && n != "undefined" && n != null && n != "null" && n!= ""){
        return false;
    }
    else{
        return true;
    }
}

/**
 * 压缩传递參數
 * params: {
                sortJsonArray: encode64(utf16to8(JSON.stringify([]))),
                conditionJsonArray: encode64(utf16to8(JSON.stringify($scope.conditionJsonArray))),
                pageNum: $scope.currentPage - 1,
                pageSize: $scope.pageSize
            }
 */

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
//将Ansi编码的字符串进行Base64编码
function encode64(input) {
    if(isNull(input)){
        return;
    }
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
            + keyStr.charAt(enc3) + keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);
    return output;
}

function utf16to8(str) {
    if(isNull(str)){
        return;
    }
    var out, i, len, c;


    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out;
}

/*// 使用
 var date = new Date(2011, 11, 31); // 注意：JS 中月的取值范围为 0~11
 var weekOfYear = date.getWeekOfYear(); // 当前日期是本年度第几周
 var weekOfMonth = date.getWeekOfMonth(); // 当前日期是本月第几周
 // 2011 年度有几周
 (new Date(2011, 11, 31)).getWeekOfYear();
 // 2011 年度 1 月有几周
 (new Date(2011, 0, 31)).getWeekOfMonth();*/

//格式化时间
Date.prototype.format = function (format) {
    if(!format){
        format = "yyyy-MM-dd hh:mm:ss";
    }

    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4- RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]: ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

/**
 * 传入一个节点对象和子节点字段，通过递归循环的方式获取所有的叶子节点的数组
 * 默认节点的子节点为children
 * @param format
 * @returns {*}
 */
function getTreeLeafs(node,children) {
    var data = [];
    data.push(node);
    if(!children){
        children = 'children';
    }
    if(node[children] && node[children].length>0){
        for(var i = 0 ; i < node[children].length ; i++){
            var dataChildren = getTreeLeafs(node[children][i],children);
            if(dataChildren.length > 0){
                for(var k = 0 ; k < dataChildren.length ; k++){
                    data.push(dataChildren[k]);
                }
            }
        }
    }
    return data;
};

/**
 * 获取扇形360度坐标点的位置
 * @param format
 * @returns {*}
 */
function getCirclePoint(x,y,r,n) {
    var data = [];
    for(var i = 1 ; i <= n ; i++){
        var ytemp = Math.sin(2*Math.PI*(i-1)/n)*r;
        var xtemp = Math.cos(2*Math.PI*(i-1)/n)*r;
        //var ytemp = Math.sin(2*Math.PI/360*(120*i/(n+1)+30))*r;
        //var xtemp = Math.cos(2*Math.PI/360*(120*i/(n+1)+30))*r;
        data.push({
            x:x+xtemp,
            y:y-ytemp
        })
    }
    return data;
}


/**
 * 设置弹出模态框
 */
function sckWindow(options) {
    if(!options){
        options={size:'',url:null,sureBtn:false,onSureClick:null,afterWinOpen:null};
    }
    $('.skyModal').removeClass('modal-lg'); //大窗口
    $('.skyModal').removeClass('modal-sm'); //小窗口
    if(options.size == 'sm'){
        $('.skyModal').addClass('modal-sm');
    } else if(options.size == 'lg'){
        $('.skyModal').addClass('modal-lg');
    }

    if(options.url && ''!=options.url){
        $('.modal-body-skyWindow').load(options.url);
    } else {
        $('.modal-body-skyWindow').html('');
    }

    $('#skyWindow').modal();
    if(options.sureBtn){
        //显示取消
        $('#skyWinN').show();
    } else {
        //隐藏取消
        $('#skyWinN').hide();
    }

    $('#skyWinY').unbind().bind('click',function () {
        if(options.sureBtn && options.onSureClick && options.onSureClick!=''){
            options.onSureClick();
        } else {
            $('#skyWindow').modal('hide');
        }
    });

    $('#skyWindow').on('shown.bs.modal',function () {
        if(options.afterWinOpen && options.afterWinOpen!=''){
            options.afterWinOpen();
        }
    });
    $('#skyWindow').on('hide.bs.modal',function () {
        options={size:'',url:null,sureBtn:false,onSureClick:null,afterWinOpen:null};
    });
}


function closeSkyWindow(){
    $('#skyWindow').modal('hide');
}

function skyAlert(msg) {
    $('.modal-body-skyalert').html(msg);
    $('#skyAlert').modal();
}

function skyConform(msg, fuc) {
    $('.modal-body-skyconform').html(msg);
    $('#skyConformY').unbind().bind('click',function () {
        $('#skyConform').modal('hide');
        fuc();
    })
    $('#skyConform').modal();
}

/**
 * 解决冒泡问题
 * @param method
 * @param scope
 */
function $binding(method, scope) {
    return (function (method,scope) {
        return function () {
            return method.apply(scope || window,arguments);
        }
    })(method,scope);
}